package dao

import (
	"errors"
	__ "order_srv/basic/orderproto"
	"order_srv/handler/models"
)

// 订单取消删除
func OrderCancellationDel(in *__.OrderCancellationDelReq) (*models.OrderCancellation, error) {
	var orderCancellation models.OrderCancellation
	orderCancellation.UserId = in.UserId
	err := orderCancellation.GetCancelDetail(int(in.Id))
	if err != nil {
		return nil, errors.New("取消订单查询失败")
	}
	if orderCancellation.Id == 0 {
		return nil, errors.New("该取消订单不存在")
	}

	if err = orderCancellation.DeleteCancel(); err != nil {
		return nil, errors.New("订单删除失败")
	}
	return &orderCancellation, nil
}

// 订单取消详情
func OrderCancellationDetail(in *__.OrderCancellationDetailReq) (*models.OrderCancellation, error) {
	var orderCancellation models.OrderCancellation
	if err := orderCancellation.GetCancelDetail(int(in.Id)); err != nil {
		return nil, errors.New("订单详情查询失败")
	}

	if orderCancellation.Id == 0 {
		return nil, errors.New("该订单不存在")
	}

	return &orderCancellation, nil
}

// 订单取消状态列表
func OrderCancellationList(in *__.OrderCancellationListReq) ([]*__.OrderCancellationList, error) {
	var orderCancellation models.OrderCancellation
	var orderCancellationList []*__.OrderCancellationList
	orderCancellation.Status = uint8(in.Status)
	list, err := orderCancellation.GetCancelList(int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, errors.New("订单取消列表查询失败")
	}
	for _, cancellation := range list {
		orderCancellationList = append(orderCancellationList, &__.OrderCancellationList{
			Id:              cancellation.Id,
			OrderId:         cancellation.OrderId,
			Reason:          cancellation.Reason,
			Liability:       uint64(cancellation.Liability),
			CancellationFee: float32(cancellation.CancellationFee),
			UserId:          cancellation.UserId,
			Identity:        int32(cancellation.Identity),
			Status:          uint64(cancellation.Status),
		})
	}
	return orderCancellationList, nil
}
